Flutter 自定义组件之导航栏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//  @Description: 自定义组件
// @Author: 陈胜辉
// @Date: 2019-12-05 08:37:35
// @Version: 版本号, YYYY-MM-DD
// @LastEditors: 陈胜辉
// @LastEditTime: 2019-12-05 08:38:46
// @Deprecated: 否
// 备注

import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/tool/data_helper.dart';
import 'package:flutter_app/util/routerUtil.dart';

/// MARK:自定义导航栏
// @LastEditors: 陈胜辉
// @Version: 版本号, YYYY-MM-DD
// @param {type}
// @return:
// @Deprecated: 否
// 备注
class CustomNav extends StatelessWidget {
CustomNav({
Key key,
this.height,
this.defaultLeft = true,
this.opacity = 1.0,
this.sapce = 5.0,
this.middleText = '',
this.middle,
this.backgroundColor,
this.background,
this.actionsMaxW,
this.leftActions,
this.rightActions,
}) : assert(height == null || height >= CommonData.navH,
'导航栏高度最小为${CommonData.navH}'),
super(key: key);

//高度
final double height;
//默认左侧按钮
final bool defaultLeft;
//屏幕两边空隙
final double sapce;
//透明度
final double opacity;
//标题文字
final String middleText;
//中间视图
final Widget middle;
//背景颜色
final Color backgroundColor;
//背景视图
final Widget background;
//默认 _actionW * count 如果超过需要设置
final double actionsMaxW;
//按钮集合
final List<Widget> leftActions;
final List<Widget> rightActions;

@override
Widget build(BuildContext context) {
//配置视图
return _handleWidget(context);
}

/// MARK:处理组件
// @LastEditors: 陈胜辉
// @Version: 版本号, YYYY-MM-DD
// @param {type}
// @return:
// @Deprecated: 否
// 备注
Widget _handleWidget(BuildContext context) {
double _actionW = 48.0;
//数据处理
//背景颜色
Color _backgroundColor = (this.backgroundColor != null)
? this.backgroundColor
: Color.fromRGBO(97, 148, 244, 1);
//透明度(0 ~ 1)
double _opacity = max(min(this.opacity, 1), 0);
//高度
double _height = (height == null) ? CommonData.navAndStatusH : height;
_height = max(CommonData.navH, _height);
//事件宽度
double _actionsMaxW = this.actionsMaxW;
//左侧组件集合
List<Widget> _leftActions = this.leftActions;
//默认左侧返回按钮
if (null == _leftActions && defaultLeft) {
_leftActions = [_getNavBack(context)];
}

//取出有多少个按钮
int count = max((_leftActions != null) ? _leftActions.length : 0,
(this.rightActions != null) ? this.rightActions.length : 0);

if (null == _actionsMaxW) {
//默认
_actionsMaxW = count * _actionW;
}

//中间组件
Widget _middle = this.middle;
if (null == _middle) {
_middle = Container(
alignment: Alignment.center,
child: Text(
middleText,
style: TextStyle(
color: Colors.white,
fontSize: 18,
),
),
);
}

// 返回组件
return Opacity(
opacity: _opacity,
child: Stack(
children: <Widget>[
//底部视图
Container(
width: CommonData.screenW,
height: _height,
color: backgroundColor,
child: Stack(
children: <Widget>[
//底部背景
Container(
width: CommonData.screenW,
height: _height,
color: _backgroundColor,
child: background,
),
//上方内容
Container(
margin: EdgeInsets.only(top: _height - CommonData.navH),
height: CommonData.navH,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
//左边
Container(
width: _actionsMaxW,
margin: EdgeInsets.only(left: sapce),
child: (null == _leftActions)
? null
: Row(
children: _leftActions,
),
),

//中间视图
Expanded(
child: _middle,
),

//右边
Container(
width: _actionsMaxW,
margin: EdgeInsets.only(right: sapce),
child: (null == rightActions)
? null
: Row(
children: rightActions,
),
),
],
),
),
],
),
),
],
),
);
}

/// MARK:导航栏按钮
// @LastEditors: 陈胜辉
// @Version: 版本号, YYYY-MM-DD
// @param {type}
// @return:
// @Deprecated: 否
// 备注
Widget _getNavBack(BuildContext context) {
return BackButton(
color: Colors.white,
onPressed: () {
RouterUtil.pop(context);
},
);
// CupertinoButton(
// padding: EdgeInsets.only(left: 0),
// pressedOpacity: 0.8,
// child: Icon(
// Icons.arrow_back_ios,
// size: 20,
// color: Colors.white,
// ),
// onPressed: () {
// RouterUtil.pop(context);
// },
// );
}
}
-------------本文结束感谢阅读-------------